﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Utility.Log;
using System.Web;
using Utility;
using Utility.Model;

namespace BaseUI
{
    public class BasePage : Page
    {
        /// <summary>
        /// 登录名
        /// </summary>
        private string _account;
        /// <summary>
        /// 应用程序URL路径
        /// </summary>
        private string _urlBase;
        /// <summary>
        /// 应用程序名
        /// </summary>
        private string _strApplicationPath;
        /// <summary>
        /// 用户信息
        /// </summary>
        private UserSession _userInfo;
        /// <summary>
        /// 用户信息
        /// </summary>
        public UserSession UserSessionInfo
        {
            get
            {
                return _userInfo;
            }
        }
        /// <summary>
        /// 应用程序URL路径
        /// </summary>
        public string UrlBase
        {
            get { return _urlBase; }
        }
        /// <summary>
        /// IP
        /// </summary>
        public string IP
        {
            get
            {
                string _ip;
                if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
                {
                    _ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
                }
                else// not using proxy or can't get the Client IP
                {
                    _ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
                }
                return _ip;
            }
        }
        /// <summary>
        /// 登录名
        /// </summary>
        public string Account
        {
            get { return _account; }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public BasePage()
        {
            _urlBase = @"http://" + Context.Request.Url.Authority.Trim();
            _strApplicationPath = Context.Request.ApplicationPath.Replace("/", "");
            _account = Page.User.Identity.Name.ToLower();
            if (!_account.IsNullOrEmpty())
            {
                _userInfo = UserSessionService.GetUserInfo(_account, IP);
            }
        }

        override protected void OnInit(EventArgs e)
        {
            if (_userInfo == null || _account.IsNullOrEmpty())
            {
                string url = _urlBase + "/" + _strApplicationPath + "Login.aspx";
                string script = " alert('提示：您未登陆或登陆时间已过期,请进行登陆');   top.location.href='" + url + "';";
                HttpContext.Current.Response.Write("<script language=\"javascript\" type=\"text/javascript\" defer>\n" + script + "\n</script>\n");
                HttpContext.Current.Response.End();
            }
            base.OnInit(e);
        }

        override protected void OnError(EventArgs e)
        {
            Exception objExp = HttpContext.Current.Server.GetLastError();
            LogHelper.WriteError("\r\n账号:" + Page.User.Identity.Name + "\r\n客户机IP:" + Request.UserHostAddress + "\r\n错误地址:" + Request.Url + "\r\n异常信息:" + Server.GetLastError().Message, objExp);
            base.OnError(e);
        }

        /// <summary>
        /// 弹出警告信息
        /// </summary>
        /// <param name="strMessage"></param>
        protected void ShowAlert(string strMessage)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "ActionError", "<script language='javascript'>alert('" + strMessage.Replace("'", "").Replace("\r\n", "") + "');</script>");
        }

        /// <summary>
        /// 弹出警告信息,调用前台JavaScript
        /// </summary>
        /// <param name="strMessage"></param>
        protected void ShowAlertScript(string strMessage)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "ActionScript", "<script language='javascript'>" + strMessage + "</script>");
        }
    }

}
